package main

import (
	"fmt"
	"math"
	"math/rand"
	"strings"
	"time"
)

/**
*  User: zhanbei
*  Date: 2025/10/27
*  Description: CS:GO炸弹爆炸动画 - C4炸弹安装与爆炸效果
 */

// Particle 表示一个粒子
type Particle struct {
	x       float64
	y       float64
	vx      float64 // x方向速度
	vy      float64 // y方向速度
	char    string
	color   int
	life    int // 生命值
	maxLife int
}

// C4炸弹的ASCII艺术
var c4BombArt = []string{
	"     _______________",
	"    /              /|",
	"   /    C4 BOMB   / |",
	"  /______________/  |",
	"  |  ┌────────┐ |  |",
	"  |  │ 00:00  │ |  /",
	"  |  └────────┘ | /",
	"  |______________|/",
	"   [████████████]",
}

// 显示炸弹安装动画
func showBombPlanting(width, height int) {
	centerX := width / 2
	centerY := height / 2

	// 显示 "Planting..." 消息
	clearScreen()
	moveCursor(centerX-10, centerY-8)
	setColor(93) // 亮黄色
	fmt.Println(">>> PLANTING C4 BOMB <<<")
	resetColor()

	// 显示安装进度条
	moveCursor(centerX-15, centerY-6)
	fmt.Print("Progress: [")
	barY := centerY - 6
	barX := centerX - 15 + 11

	for i := 0; i <= 30; i++ {
		moveCursor(barX+i, barY)
		setColor(92) // 亮绿色
		fmt.Print("█")
		resetColor()
		time.Sleep(100 * time.Millisecond)
	}
	moveCursor(barX+31, barY)
	fmt.Print("]")

	time.Sleep(300 * time.Millisecond)

	// 显示 "Bomb has been planted"
	clearScreen()
	moveCursor(centerX-15, centerY-5)
	setColor(91) // 亮红色
	fmt.Println("╔═══════════════════════════╗")
	moveCursor(centerX-15, centerY-4)
	fmt.Println("║  BOMB HAS BEEN PLANTED!  ║")
	moveCursor(centerX-15, centerY-3)
	fmt.Println("╚═══════════════════════════╝")
	resetColor()

	time.Sleep(1 * time.Second)
}

// 显示炸弹和倒计时
func showBombCountdown(width, height int) {
	centerX := width / 2
	centerY := height / 2

	// 倒计时音效提示
	beepSounds := []string{
		"beep...",
		"beep... beep...",
		"beep! beep! beep!",
		"BEEP! BEEP! BEEP!",
	}

	for countdown := 3; countdown > 0; countdown-- {
		clearScreen()

		// 绘制炸弹
		bombStartY := centerY - 6
		for i, line := range c4BombArt {
			moveCursor(centerX-len(line)/2, bombStartY+i)
			if i >= 4 && i <= 6 {
				setColor(91) // 红色显示屏
			} else {
				setColor(37) // 白色
			}
			// 如果是显示屏行，显示倒计时
			if i == 5 {
				displayLine := strings.Replace(line, "00:00", fmt.Sprintf("00:0%d", countdown), 1)
				fmt.Print(displayLine)
			} else {
				fmt.Print(line)
			}
		}
		resetColor()

		// 显示音效文字
		beepIdx := 3 - countdown
		if beepIdx >= len(beepSounds) {
			beepIdx = len(beepSounds) - 1
		}
		moveCursor(centerX-len(beepSounds[beepIdx])/2, centerY+5)
		setColor(93) // 黄色
		fmt.Print(beepSounds[beepIdx])
		resetColor()

		// 警告文字闪烁
		if countdown <= 2 {
			moveCursor(centerX-10, centerY+7)
			setColor(91)
			fmt.Print("⚠  GET OUT OF THERE! ⚠")
			resetColor()
		}

		time.Sleep(1 * time.Second)
	}
}

// RunBang CS:GO炸弹爆炸动画
func RunBang() {
	rand.Seed(time.Now().UnixNano())

	width, height := getTerminalSize()

	// 隐藏光标
	hideCursor()
	defer showCursor()

	// 清屏
	clearScreen()

	centerX := float64(width) / 2
	centerY := float64(height) / 2

	// 第一阶段：安装炸弹
	showBombPlanting(width, height)

	// 第二阶段：倒计时（3秒）
	showBombCountdown(width, height)

	// 第三阶段：爆炸！
	clearScreen()
	moveCursor(int(centerX)-10, int(centerY))
	setColor(91)
	fmt.Print("💣 TERRORISTS WIN! 💣")
	resetColor()
	time.Sleep(500 * time.Millisecond)

	clearScreen()

	// 创建粒子
	particles := make([]*Particle, 0)
	chars := []string{"*", "#", "@", "%", "&", "+", "=", "~", "^", "o", "O", "●", "◉", "◆", "■", "▲", "▼"}
	colors := []int{91, 92, 93, 94, 95, 96, 97} // 各种亮色

	// 生成爆炸粒子
	for i := 0; i < 200; i++ {
		angle := rand.Float64() * 2 * math.Pi
		speed := rand.Float64()*3 + 1
		p := &Particle{
			x:       centerX,
			y:       centerY,
			vx:      math.Cos(angle) * speed,
			vy:      math.Sin(angle) * speed * 0.5, // y方向速度减半，因为终端字符是矩形
			char:    chars[rand.Intn(len(chars))],
			color:   colors[rand.Intn(len(colors))],
			life:    50,
			maxLife: 50,
		}
		particles = append(particles, p)
	}

	// 动画循环
	ticker := time.NewTicker(50 * time.Millisecond)
	defer ticker.Stop()

	frameCount := 0
	maxFrames := 60

	for frameCount < maxFrames {
		<-ticker.C
		frameCount++

		// 清屏
		for row := 1; row <= height; row++ {
			moveCursor(1, row)
			for col := 0; col < width; col++ {
				fmt.Print(" ")
			}
		}

		// 更新并绘制粒子
		activeParticles := make([]*Particle, 0)
		for _, p := range particles {
			// 更新位置
			p.x += p.vx
			p.y += p.vy

			// 添加重力效果
			p.vy += 0.1

			// 减少生命值
			p.life--

			// 只保留还活着的粒子
			if p.life > 0 {
				activeParticles = append(activeParticles, p)

				// 绘制粒子
				screenX := int(p.x)
				screenY := int(p.y)

				if screenX >= 1 && screenX <= width && screenY >= 1 && screenY <= height {
					moveCursor(screenX, screenY)
					// 根据生命值调整颜色强度
					if p.life < 10 {
						setColor(p.color - 60) // 变暗
					} else {
						setColor(p.color)
					}
					fmt.Print(p.char)
				}
			}
		}
		particles = activeParticles

		resetColor()

		// 如果没有粒子了，提前退出
		if len(particles) == 0 {
			break
		}
	}

	// 清屏并显示结束信息
	clearScreen()
	moveCursor(int(centerX)-8, int(centerY))
	setColor(91)
	fmt.Println("💥 BOOM! 💥")
	resetColor()
	moveCursor(1, height)
	time.Sleep(2 * time.Second)
}
